/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vertexnet.realestate.view.panel;

import static com.vertexnet.realestate.constants.UIConstants.ALL;

import java.awt.Component;
import java.awt.Container;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import com.vertexnet.realestate.constants.ReportTypes;
import com.vertexnet.realestate.interfaces.Controller;
import com.vertexnet.realestate.interfaces.DefaultPanel;
import com.vertexnet.realestate.view.utility.DefaultTable;
import com.vertexnet.realestatenet.model.bean.ReportBean;

/**
 *
 * @author Srivathsa
 */
public class ReportsPanel extends javax.swing.JPanel implements DefaultPanel {

	private static final long serialVersionUID = 1L;
	private Controller controller;
	private ReportBean reportBean = new ReportBean();

	/**
     * Creates new form ReportsPanel
     */
    public ReportsPanel(Controller controller) {
        initComponents();
        this.controller = controller;
        employeeDataCheckBox.setVisible(false);
        
        ReportTypes[] types = ReportTypes.values();
        reportTypeComboBox.setModel(new DefaultComboBoxModel(types));
		setupPanel(types[0]);
    }

	private void setupPanel(ReportTypes type) {
		ReportBean reportBean = type.getReportBean();
		this.reportBean = reportBean;

		if (reportBean.isCompleteReport()) {
			completeRadioButton.setSelected(true);
			displayDateFields(false);
		} else {
			specificRadioButton.setSelected(true);
			fromDateChooser.setDate(reportBean.getFromDate());
			toDateChooser.setDate(reportBean.getToDate());
			displayDateFields(true);
		}

		customerDataCheckBox.setSelected(reportBean.isCustomers());
		customerDetailsPanel.setVisible(reportBean.isCustomers());

		agentsDataCheckBox.setSelected(reportBean.isAgents());

		overDueCheckBox.setSelected(reportBean.isOverDue());
		dueCheckBox.setSelected(reportBean.isDue());
		paidCheckBox.setSelected(reportBean.isPaid());
		
		boolean enabled = type.equals(ReportTypes.CUSTOM);
		setComponentEnabled(enabled, this);
		reportTypeComboBox.setEnabled(true);
		allReportsButton.setEnabled(true);
		this.repaint();
	}
	
	private void setComponentEnabled(boolean enabled, Component component) {
		component.setEnabled(enabled);
		if (component instanceof Container) {
			Component[] components = ((Container) component).getComponents();
			if (components != null && components.length > 0) {
				for (Component heldComponent : components) {
					setComponentEnabled(enabled, heldComponent);
				}
			}
		}
	}

    void displayDateFields(boolean val) {
		fromLabel.setVisible(val);
		toLabel.setVisible(val);
    	fromDateChooser.setVisible(val);
		toDateChooser.setVisible(val);
    }
    

	/**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        durationPanel = new javax.swing.JPanel();
        completeRadioButton = new javax.swing.JRadioButton();
        specificRadioButton = new javax.swing.JRadioButton();
        fromDateChooser = new com.toedter.calendar.JDateChooser();
        toDateChooser = new com.toedter.calendar.JDateChooser();
        fromLabel = new javax.swing.JLabel();
        toLabel = new javax.swing.JLabel();
        reportTypeComboBox = new javax.swing.JComboBox();
        reportTypeLabel = new javax.swing.JLabel();
        allReportsButton = new javax.swing.JButton();
        customerDetailsPanel = new javax.swing.JPanel();
        overDueCheckBox = new javax.swing.JCheckBox();
        dueCheckBox = new javax.swing.JCheckBox();
        paidCheckBox = new javax.swing.JCheckBox();
        includePanel = new javax.swing.JPanel();
        customerDataCheckBox = new javax.swing.JCheckBox();
        agentsDataCheckBox = new javax.swing.JCheckBox();
        employeeDataCheckBox = new javax.swing.JCheckBox();

        durationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Duration"));

        buttonGroup1.add(completeRadioButton);
        completeRadioButton.setText("Complete");
        completeRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                completeRadioButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(specificRadioButton);
        specificRadioButton.setSelected(true);
        specificRadioButton.setText("Specific Duration");
        specificRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                specificRadioButtonActionPerformed(evt);
            }
        });

        fromDateChooser.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fromDateChooserPropertyChange(evt);
            }
        });

        toDateChooser.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                toDateChooserPropertyChange(evt);
            }
        });

        fromLabel.setText("From : ");

        toLabel.setText("To : ");

        reportTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Custom", "Complete", "Yearly", "Monthly", "Daily", "Complete Profit", "Complete Loss", "Yearly Profit", "Yearly Loss", "Monthly Profit", "Monthly Loss", "Daily Profit", "Daily Loss" }));
        reportTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reportTypeComboBoxActionPerformed(evt);
            }
        });

        reportTypeLabel.setText("Report Type");

        javax.swing.GroupLayout durationPanelLayout = new javax.swing.GroupLayout(durationPanel);
        durationPanel.setLayout(durationPanelLayout);
        durationPanelLayout.setHorizontalGroup(
            durationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(durationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(durationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(durationPanelLayout.createSequentialGroup()
                        .addComponent(completeRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(specificRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 170, Short.MAX_VALUE)
                        .addComponent(reportTypeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(reportTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(durationPanelLayout.createSequentialGroup()
                        .addComponent(fromLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fromDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addComponent(toLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(toDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );

        durationPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {fromDateChooser, toDateChooser});

        durationPanelLayout.setVerticalGroup(
            durationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(durationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(durationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(completeRadioButton)
                    .addComponent(specificRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(durationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(fromLabel)
                    .addComponent(fromDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toLabel)
                    .addComponent(toDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(durationPanelLayout.createSequentialGroup()
                .addGroup(durationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(reportTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reportTypeLabel))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        allReportsButton.setText("Generate Report");
        allReportsButton.setName(ALL.getReport());
        allReportsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allReportsButtonActionPerformed(evt);
            }
        });

        customerDetailsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer Details"));

        overDueCheckBox.setSelected(true);
        overDueCheckBox.setText("Over Due");
        overDueCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                overDueCheckBoxActionPerformed(evt);
            }
        });

        dueCheckBox.setSelected(true);
        dueCheckBox.setText("Due");
        dueCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dueCheckBoxActionPerformed(evt);
            }
        });

        paidCheckBox.setSelected(true);
        paidCheckBox.setText("Paid");
        paidCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paidCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout customerDetailsPanelLayout = new javax.swing.GroupLayout(customerDetailsPanel);
        customerDetailsPanel.setLayout(customerDetailsPanelLayout);
        customerDetailsPanelLayout.setHorizontalGroup(
            customerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customerDetailsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(customerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(overDueCheckBox)
                    .addComponent(dueCheckBox)
                    .addComponent(paidCheckBox))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        customerDetailsPanelLayout.setVerticalGroup(
            customerDetailsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customerDetailsPanelLayout.createSequentialGroup()
                .addComponent(overDueCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dueCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(paidCheckBox))
        );

        includePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Include"));

        customerDataCheckBox.setSelected(true);
        customerDataCheckBox.setText("Customer Data");
        customerDataCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                customerDataCheckBoxActionPerformed(evt);
            }
        });

        agentsDataCheckBox.setSelected(true);
        agentsDataCheckBox.setText("Agents Data");
        agentsDataCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agentsDataCheckBoxActionPerformed(evt);
            }
        });

        employeeDataCheckBox.setSelected(true);
        employeeDataCheckBox.setText("Employees Data");
        employeeDataCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                employeeDataCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout includePanelLayout = new javax.swing.GroupLayout(includePanel);
        includePanel.setLayout(includePanelLayout);
        includePanelLayout.setHorizontalGroup(
            includePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(includePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(includePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(customerDataCheckBox)
                    .addComponent(agentsDataCheckBox)
                    .addComponent(employeeDataCheckBox))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        includePanelLayout.setVerticalGroup(
            includePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(includePanelLayout.createSequentialGroup()
                .addComponent(customerDataCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(agentsDataCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(employeeDataCheckBox))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(customerDetailsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(durationPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(includePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(allReportsButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(durationPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(includePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(customerDetailsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(allReportsButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void allReportsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allReportsButtonActionPerformed
    	controller.handleEvent(evt);
    }//GEN-LAST:event_allReportsButtonActionPerformed

    private void completeRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_completeRadioButtonActionPerformed
    	displayDateFields(false);
    	reportBean.setFromDate(null);
    	reportBean.setToDate(null);
    }//GEN-LAST:event_completeRadioButtonActionPerformed

    private void specificRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_specificRadioButtonActionPerformed
    	displayDateFields(true);
    	fromDateChooser.setDate(reportBean.getFromDate());
    	toDateChooser.setDate(reportBean.getToDate());
    }//GEN-LAST:event_specificRadioButtonActionPerformed

    private void reportTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reportTypeComboBoxActionPerformed
    	ReportTypes type = (ReportTypes) reportTypeComboBox.getSelectedItem();
    	setupPanel(type);
    }//GEN-LAST:event_reportTypeComboBoxActionPerformed

    private void customerDataCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_customerDataCheckBoxActionPerformed
		dueCheckBox.setSelected(reportBean.isDue());
		paidCheckBox.setSelected(reportBean.isPaid());
		overDueCheckBox.setSelected(reportBean.isOverDue());
		reportBean.setCustomers(customerDataCheckBox.isSelected());
    	customerDetailsPanel.setVisible(customerDataCheckBox.isSelected());
    }//GEN-LAST:event_customerDataCheckBoxActionPerformed

    private void overDueCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_overDueCheckBoxActionPerformed
        reportBean.setOverDue(overDueCheckBox.isSelected());
    }//GEN-LAST:event_overDueCheckBoxActionPerformed

    private void dueCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dueCheckBoxActionPerformed
        reportBean.setDue(dueCheckBox.isSelected());
    }//GEN-LAST:event_dueCheckBoxActionPerformed

    private void paidCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paidCheckBoxActionPerformed
        reportBean.setPaid(paidCheckBox.isSelected());
    }//GEN-LAST:event_paidCheckBoxActionPerformed

    private void agentsDataCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agentsDataCheckBoxActionPerformed
    	reportBean.setAgents(agentsDataCheckBox.isSelected());
    }//GEN-LAST:event_agentsDataCheckBoxActionPerformed

	private void fromDateChooserPropertyChange(java.beans.PropertyChangeEvent evt) {// GEN-FIRST:event_fromDateChooserPropertyChange
		if (toDateChooser.getDate() != null && fromDateChooser.getDate() != null && fromDateChooser.getDate().after(toDateChooser.getDate())) {
			fromDateChooser.setDate(reportBean.getFromDate());
			JOptionPane.showMessageDialog(this, "From Date can not be After To Date");
		} else {
			reportBean.setFromDate(fromDateChooser.getDate());
		}
	}// GEN-LAST:event_fromDateChooserPropertyChange

	private void toDateChooserPropertyChange(java.beans.PropertyChangeEvent evt) {// GEN-FIRST:event_toDateChooserPropertyChange
		if (fromDateChooser.getDate() != null && toDateChooser.getDate() != null && toDateChooser.getDate().before(fromDateChooser.getDate())) {
			toDateChooser.setDate(reportBean.getToDate());
			JOptionPane.showMessageDialog(this, "To Date can not be Before From Date");
		} else {
			reportBean.setToDate(toDateChooser.getDate());
		}
	}// GEN-LAST:event_toDateChooserPropertyChange

	private void employeeDataCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_employeeDataCheckBoxActionPerformed
	}// GEN-LAST:event_employeeDataCheckBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox agentsDataCheckBox;
    private javax.swing.JButton allReportsButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton completeRadioButton;
    private javax.swing.JCheckBox customerDataCheckBox;
    private javax.swing.JPanel customerDetailsPanel;
    private javax.swing.JCheckBox dueCheckBox;
    private javax.swing.JPanel durationPanel;
    private javax.swing.JCheckBox employeeDataCheckBox;
    private com.toedter.calendar.JDateChooser fromDateChooser;
    private javax.swing.JLabel fromLabel;
    private javax.swing.JPanel includePanel;
    private javax.swing.JCheckBox overDueCheckBox;
    private javax.swing.JCheckBox paidCheckBox;
    private javax.swing.JComboBox reportTypeComboBox;
    private javax.swing.JLabel reportTypeLabel;
    private javax.swing.JRadioButton specificRadioButton;
    private com.toedter.calendar.JDateChooser toDateChooser;
    private javax.swing.JLabel toLabel;
    // End of variables declaration//GEN-END:variables

	public JButton getAddButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public JButton getEditButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public JButton getDeleteButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public DefaultTable getTable() {
		// TODO Auto-generated method stub
		return null;
	}

	public JButton getReportButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public JButton getMakePaymentButton() {
		// TODO Auto-generated method stub
		return null;
	}

	public ReportBean getReportBean() {
		return reportBean;
	}
}
